package com.lc.luzhengtang.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.PersonalShopListAdapter;
import com.lc.luzhengtang.banner.BannerLayout;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.bean.MyShopBean;
import com.lc.luzhengtang.bean.PersonalShopListBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnAddShop;
import com.lc.luzhengtang.conn.ConnMyShopBanner;
import com.lc.luzhengtang.conn.ConnShopTypeList;
import com.lc.luzhengtang.glide.GlideImageLoader;
import com.lc.luzhengtang.layoutmanager.FullyGridLayoutManager;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

@SuppressLint("ValidFragment")
public class SimpleShoppingFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.card_title_tv)
    TextView cardTitleTv;
    @BindView(R.id.banner2)
    BannerLayout banner2;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    Unbinder unbinder;
    private String id;
    PersonalShopListAdapter mIndexAdapter;
    private int currentPage = 1;


    List<PersonalShopListBean.DataBeanX.DataBean> results;
    List<MyShopBean.DataBean> bannerBeen = new ArrayList<>();
    List<String> urls = new ArrayList<>();

    public static SimpleShoppingFragment getInstance(String id) {
        SimpleShoppingFragment sf = new SimpleShoppingFragment();
        sf.id = id;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fr_dianpu, null);

        TextView card_title_tv = (TextView) v.findViewById(R.id.card_title_tv);
        card_title_tv.setText(id);


        unbinder = ButterKnife.bind(this, v);
        //   getdianpuData();
     /*   if (id.equals("1")){
            banner2.setVisibility(View.VISIBLE);
        }else {
            banner2.setVisibility(View.GONE);
        }*/
        banner2.setVisibility(View.VISIBLE);
        initData();
        return v;
    }


    protected void initData() {
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new FullyGridLayoutManager(getActivity(), 2));

        mIndexAdapter = new PersonalShopListAdapter(null);
        mIndexAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        mIndexAdapter.isFirstOnly(false);
      /*  Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider);
        recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 1));*/

        recyclerView.setAdapter(mIndexAdapter);

        refreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        refreshLayout.setOnRefreshListener(this);
        mIndexAdapter.setOnLoadMoreListener(this);
        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }

    List<String> empty = new ArrayList<>();

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

    public void getShopBannerData() {
        new ConnMyShopBanner(id, new AsyCallBack<MyShopBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, MyShopBean myShopBean) throws Exception {
                super.onSuccess(toast, type, o, myShopBean);
                bannerBeen.clear();
                //  bannerBeen = myShopBean.getData();
                bannerBeen.addAll(myShopBean.getData());
                empty.add("https://git.oschina.net/yuanchong121/LuZhengTang/blob/master/app/src/main/res/mipmap-hdpi/zhanwei.png");
                urls.clear();
                for (int i = 0; i < bannerBeen.size(); i++) {
                    urls.add(Conn.HOST3 + bannerBeen.get(i).getPicurl());

                }
                banner2.setImageLoader(new GlideImageLoader());
                if (urls.isEmpty()) {
                    banner2.setViewUrls(empty);
                } else {
                    banner2.setViewUrls(urls);
                }
                if (bannerBeen.isEmpty()) {

                } else {

                    banner2.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                        @Override
                        public void onItemClick(int position) {
                            // Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent();

                            intent.putExtra("url", bannerBeen.get(position).getWeburl());
                            intent.putExtra("title", bannerBeen.get(position).getTitle());
                            intent.setClass(getActivity(), IndexWebActivity.class);
                            startActivity(intent);
                        }
                    });
                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
            }
        }


        ).execute(getActivity(), false);
    }


    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();

    }


    @Override
    public void onRefresh() {
        empty.clear();
        new ConnShopTypeList(BaseApplication.basePreferences.readUid(), id, 1, new AsyCallBack<PersonalShopListBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, Object o, final PersonalShopListBean personalShopListBean) throws Exception {
                super.onSuccess(toast, type, o, personalShopListBean);
                results = personalShopListBean.getData().getData();
                // results.addAll(info.data);
                if (results != null) {
                    currentPage = 1;
                    mIndexAdapter.setNewData(results);
                }

                mIndexAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        //   Toast.makeText(getActivity(), "点击了" + position, Toast.LENGTH_SHORT).show();
                        new ConnAddShop(personalShopListBean.getData().getData().get(position).getId() + "", BaseApplication.basePreferences.readUid(), "1", "1", "1", new AsyCallBack<CommenBean>() {
                            @Override
                            public void onStart(int type) throws Exception {
                                super.onStart(type);
                            }

                            @Override
                            public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                                super.onSuccess(toast, type, o, commenBean);
                                //  Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                                LovelyToast.makeText(getActivity(), "成功加入购物车", 0, LovelyToast
                                        .SUCCESS, LovelyToast.SCALE);
                            }

                            @Override
                            public void onFail(String toast, int type) throws Exception {
                                super.onFail(toast, type);
                            }

                            @Override
                            public void onEnd(String toast, int type) throws Exception {
                                super.onEnd(toast, type);
                                //可能需要移除之前添加的布局
                                mIndexAdapter.removeAllFooterView();
                                //最后调用结束刷新的方法
                                setRefreshing(false);
                            }
                        }

                        ).execute(getActivity(), false);

                    }
                });


            }


            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                setRefreshing(false);
                dismissLoading();
                mIndexAdapter.setEnableLoadMore(true);
            }
        }

        ).execute(getActivity(), false);
        getShopBannerData();
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnShopTypeList(BaseApplication.basePreferences.readUid(), id, 1, new AsyCallBack<PersonalShopListBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                mIndexAdapter.setEnableLoadMore(true);


            }

            @Override
            public void onSuccess(String toast, int type, Object o, PersonalShopListBean personalShopListBean) throws Exception {
                super.onSuccess(toast, type, o, personalShopListBean);
             /*   results = personalShopListBean.getData().getData();
                // results.addAll(info.data);
                if (results != null) {
                    currentPage = 1;
                    mIndexAdapter.setNewData(results);
                }*/
                results = personalShopListBean.getData().getData();
                // results.addAll(info.data);

                if (results != null && results.size() > 0) {
                    currentPage++;
                    mIndexAdapter.setNewData(results);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    mIndexAdapter.loadMoreComplete();
                    View noDataView = getActivity().getLayoutInflater().inflate(R.layout.item_no_data, (ViewGroup) recyclerView.getParent(), false);
                    mIndexAdapter.addFooterView(noDataView);
                }
            }


            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                mIndexAdapter.removeAllFooterView();
                //最后调用结束刷新的方法
                setRefreshing(false);
                mIndexAdapter.setEnableLoadMore(false);
            }
        }

        ).execute(getActivity(), false);
    }
}